package tech.aistar.controller;

import tech.aistar.entity.Phone;
import tech.aistar.service.IPhoneService;
import tech.aistar.service.impl.PhoneServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/9/12
 * @Version: 1.0
 */
@WebServlet("/phone/add")
public class PhoneAddAction extends HttpServlet{
    private IPhoneService  phoneService = new PhoneServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        //根据name属性获取表单里的每个值
        String type_id = req.getParameter("typeId");
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String url = req.getParameter("url");
        String intro = req.getParameter("intro");
        String free = req.getParameter("free");

        //创建一个手机对象
        Phone phone = new Phone();
        //把获取的值添加到这个对象中
        phone.setTypeId(Integer.valueOf(type_id));
        phone.setName(name);
        phone.setPrice(Double.valueOf(price));
        phone.setUrl(url);
        phone.setIntro(intro);
        phone.setFree(Integer.valueOf(free));
        phone.setStatus(1);

        phoneService.add(phone);//执行添加语句添加到数据库

        resp.sendRedirect(req.getContextPath()+"/phone/index");

    }
}
